package com.hs.mod_archive.view

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.hs.mod_archive.R

object DialogUtil {
    /**
     * 要用activity ,否则  wl.type = WindowManager.LayoutParams.TYPE_TOAST 会消失
     */
    fun buildSpecialView(
        context: Activity,
        contentView: View,
        w: Int,
        h: Int,
        outsideCancelable: Boolean,
        cancelable: Boolean
    ): MyDialog {
        val dialog = innerBuildDialog(context, cancelable, outsideCancelable)
        dialog.setContentView(contentView)
        setDialogStyle(context, dialog, w, h)
        dialog.show()
        return dialog
    } fun buildSpecialView(
        context: FragmentActivity?,
        contentView: View,
        w: Int,
        h: Int,
        outsideCancelable: Boolean,
        cancelable: Boolean
    ): MyDialog {
        val dialog = innerBuildDialog(context!!, cancelable, outsideCancelable)
        dialog.setContentView(contentView)
        setDialogStyle(context, dialog, w, h)
        dialog.show()
        return dialog
    }

    private fun innerBuildDialog(
        context: Context,
        cancelable: Boolean,
        outsideTouchable: Boolean
    ): MyDialog {
        val dialog = MyDialog(context)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(cancelable)
        dialog.setCanceledOnTouchOutside(outsideTouchable)
        return dialog
    }

    fun setDialogStyle(activity: Context?, dialog: MyDialog, w: Int, h: Int) {
        val window = dialog.window
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //  keycode to show round corner
        val wl = window.attributes
        wl.width = w
        wl.height = h
        if (activity !is Activity) {
            wl.type = WindowManager.LayoutParams.TYPE_TOAST //  keycode to improve window level
        }
        dialog.onWindowAttributesChanged(wl)
    }

    /**
     * 要用activity ,否则  wl.type = WindowManager.LayoutParams.TYPE_TOAST 会消失
     */
    fun showDialogMsg(
        context: Activity,
        msg: String?,
        outsideCancelable: Boolean,
        cancelable: Boolean
    ): MyDialog {
        val dialog = innerBuildDialog(context, cancelable, outsideCancelable)
        val inflater = LayoutInflater.from(context)
        val w = context.resources.getDimensionPixelSize(R.dimen.dp_482)
        val h = context.resources.getDimensionPixelSize(R.dimen.dp_356)
        val layout_root = inflater.inflate(R.layout.dialog_msg, null, false)
        layout_root.setOnClickListener(null)
        val tv_msg = layout_root.findViewById<TextView>(R.id.tv_msg)
        tv_msg.text = msg
        val close = layout_root.findViewById<View>(R.id.bt_close)
        val sure = layout_root.findViewById<View>(R.id.bt_sure)
        close.setOnClickListener { v: View? -> dialog.dismiss() }
        sure.setOnClickListener { v: View? -> dialog.dismiss() }
        dialog.setContentView(layout_root)
        setDialogStyle(context, dialog, w, h)
        dialog.show()
        return dialog
    }

    fun showDialogAskTwoButton(
        context: Context, msg: String?, buttonCancel: String?, buttonSure: String?,
        outsideCancelable: Boolean, cancelable: Boolean,
        onButtonSureCallback: OnButtonCancelSureCallback?
    ): MyDialog {
        val dialog = innerBuildDialog(context, cancelable, outsideCancelable)
        val inflater = LayoutInflater.from(context)
        val w = context.resources.getDimensionPixelSize(R.dimen.dp_482)
        val h = context.resources.getDimensionPixelSize(R.dimen.dp_356)
        val layout_root = inflater.inflate(R.layout.dialog_ask_two_button, null, false)
        layout_root.setOnClickListener(null)
        val tv_msg = layout_root.findViewById<TextView>(R.id.tv_msg)
        tv_msg.text = msg
        val close = layout_root.findViewById<View>(R.id.bt_close)
        val bt_cancel = layout_root.findViewById<TextView>(R.id.bt_cancel)
        bt_cancel.text = buttonCancel
        val sure = layout_root.findViewById<TextView>(R.id.bt_sure)
        sure.text = buttonSure
        close.setOnClickListener { v: View? -> dialog.dismiss() }
        bt_cancel.setOnClickListener { v: View? ->
            onButtonSureCallback?.onButtonCancelCall(dialog)
        }
        sure.setOnClickListener { v: View? ->
            onButtonSureCallback?.onButtonSureCall(dialog)
        }
        dialog.setContentView(layout_root)
        setDialogStyle(context, dialog, w, h)
        dialog.show()
        return dialog
    }

    fun updateDialogMsg(activity: Context?, dialog: MyDialog?, msgText: String?) {
        if (dialog != null) {
            var tvMsg = dialog.findViewById<TextView>(R.id.tv_msg)
            if (tvMsg != null) {
                tvMsg.text = msgText
            } else {
                //tv_title
                tvMsg = dialog.findViewById(R.id.tv_title)
                if (tvMsg != null) {
                    tvMsg.text = msgText
                }
            }
        }
    }

    interface OnButtonCancelSureCallback {
        fun onButtonCancelCall(dialog: Dialog?)
        fun onButtonSureCall(dialog: Dialog?)
    }
}